package top.tangyh.lamp.care.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.jsonwebtoken.lang.Collections;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.base.R;
import top.tangyh.basic.context.ContextUtil;
import top.tangyh.basic.exception.BizException;
import top.tangyh.basic.interfaces.echo.EchoService;
import top.tangyh.basic.base.controller.SuperController;
import top.tangyh.lamp.care.entity.BHl7Route;
import top.tangyh.lamp.care.service.BExeYaoEventService;
import top.tangyh.lamp.care.service.BHl7RouteService;
import top.tangyh.lamp.care.service.BHl7YzYaoPlanService;
import top.tangyh.lamp.care.entity.BHl7YzYaoPlan;
import top.tangyh.lamp.care.vo.RevokeRecordYaoOutVO;
import top.tangyh.lamp.care.vo.query.*;
import top.tangyh.lamp.care.vo.result.ExeYaoOiOutVO;
import top.tangyh.lamp.care.vo.result.ExeYaoOutVO;
import top.tangyh.lamp.care.vo.save.BHl7YzYaoPlanSaveVO;
import top.tangyh.lamp.care.vo.save.ExeYaoEventInVO;
import top.tangyh.lamp.care.vo.save.ExeYaoInVO;
import top.tangyh.lamp.care.vo.update.BHl7YzYaoPlanUpdateVO;
import top.tangyh.lamp.care.vo.result.BHl7YzYaoPlanResultVO;
import top.tangyh.lamp.care.vo.update.UpdateExeYaoInVO;
import top.tangyh.lamp.care.vo.update.UpdateIsShowCareVO;
import top.tangyh.lamp.common.utils.SecurityUtil;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 医嘱执行计划表
 * </p>
 *
 * @author zqb
 * @date 2024-07-22 15:09:13
 * @create [2024-07-22 15:09:13] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/exe/yao")
@Tag(name = "执行用药接口")
public class BHl7YzYaoPlanController {
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }
//
//    extends SuperController<BHl7YzYaoPlanService, Integer, BHl7YzYaoPlan
//            , BHl7YzYaoPlanSaveVO, BHl7YzYaoPlanUpdateVO, BHl7YzYaoPlanPageQuery, BHl7YzYaoPlanResultVO>

    private final BExeYaoEventService bExeYaoEventService;

    private final BHl7YzYaoPlanService bHl7YzYaoPlanService;


    private final BHl7RouteService bHl7RouteService;

    @Operation(summary = "添加执行用药事件")
    @PostMapping("/event")
    public R addEvent(@RequestBody ExeYaoEventInVO inVO) {
        bExeYaoEventService.addEvent(inVO);
        return R.success();
    }


    @Operation(summary = "删除执行用药事件")
    @DeleteMapping("/event")
    public R deleteEvent(Long id) {
        bExeYaoEventService.deleteEvent(id);
        return R.success();
    }


    @Operation(summary = "查询执行用药列表")
    @PostMapping("/list")
    public R<List<ExeYaoOutVO>> selectListByQuery(@RequestBody ExeYaoQueryVO queryVO) {
        List<ExeYaoOutVO> result = bExeYaoEventService.selectExeYaoListNoEventByQuery(queryVO);
        return R.success(result);
    }


    @Operation(summary = "根据住院号,时间段查询执行用药已用量")
    @GetMapping("/used")
    public R<List<ExeYaoOiOutVO>> queryExeYaoOi(ExeYaoOiQueryVO queryVO) {
        List<ExeYaoOiOutVO> exeYaoOiOutVOS = bExeYaoEventService.countUsedByTimeRangeHospitalNumber(queryVO);
        return R.success(exeYaoOiOutVOS);
    }


    @Operation(summary = "根据主药id获取一组用药")
    @GetMapping
    public R<ExeYaoOutVO> addExeYao(@RequestParam("id") Long id) {
        return R.success(bExeYaoEventService.getYaoByMastId(id));
    }

    @Operation(summary = "补录")
    @PostMapping
    public R addExeYao(@RequestBody @Valid ExeYaoInVO inVO) {
        bExeYaoEventService.addExeYao(inVO);
        return R.success();
    }

    @Operation(summary = "修改用药详情")
    @PutMapping
    public R updateExeYao(@RequestBody @Valid UpdateExeYaoInVO inVO) {
        bExeYaoEventService.updateExeYao(inVO);
        return R.success();
    }


    @Operation(summary = "删除执行用药")
    @PutMapping("/del")
    public R deleteExeYao(@RequestBody DelExeYaoInVO inVO) {
        bExeYaoEventService.deleteExeYao(inVO);
        return R.success();
    }


    @Operation(summary = "删除指定id的药品")
    @GetMapping("/del/one/yao")
    public R deleteExeYaoOne(@RequestParam Long id, @RequestParam(required = false) Long exeTime, @Parameter(name = "updateIo", description = "是否是已执行状态", required = true) Boolean updateIo) {

        if (null != id) {
            BHl7YzYaoPlan yaoPlanPO = bHl7YzYaoPlanService.getById(Math.toIntExact(id));
            if(null!=yaoPlanPO){
                if (null != SecurityUtil.checkRecordAuth(yaoPlanPO.getCreatedBy(), yaoPlanPO.getUpdatedBy(), ContextUtil.getUserId())) {
                    throw new BizException(SecurityUtil.checkRecordAuth(yaoPlanPO.getCreatedBy(), yaoPlanPO.getUpdatedBy(), ContextUtil.getUserId()));
                }
            }

            if (null != exeTime) {
                //已执行状态的才去更改
//                if (updateIo) {,

                //暂时不做 定时任务
//                    exeYaoJob.updateInIoStaticsValueWithHospitalNumber(null, null, Arrays.asList(yaoPlanPO.getHospitalNumber()));
//                }

            }
        }
        LambdaUpdateWrapper<BHl7YzYaoPlan> lambda = new UpdateWrapper<BHl7YzYaoPlan>().lambda();
        lambda.set(BHl7YzYaoPlan::getIsDeleted, 1)
                .eq(BHl7YzYaoPlan::getId, id);
        BHl7YzYaoPlan result = bHl7YzYaoPlanService.updateById(lambda);//提交
//        if (result && null != exeTime) {
        //已执行状态的才去更改
//            if (updateIo) {
//
//            }
//        }

        return R.success();
    }

    @Operation(summary = "设置主药")
    @GetMapping("/set/master")
    public R deleteExeYaoOne(@RequestParam Long id, @RequestParam Long oldMasterId) {

        return R.success(bExeYaoEventService.updateMaster(id, oldMasterId));
    }

    @Operation(summary = "查询当前时间24小时内的用药删除记录")
    @GetMapping("/records")
    public R<List<RevokeRecordYaoOutVO>> selectListByQuery(String hospitalNumber) {
        List<RevokeRecordYaoOutVO> result = bExeYaoEventService.selectRevokeRecords(hospitalNumber);
        return R.success(result);
    }

    @Operation(summary = "撤销删除用药记录")
    @PostMapping("/records")
    public R revokeDeleteExeYao(@RequestBody List<RevokeInVO> records) {
        bExeYaoEventService.revokeDeleteExeYao(records);
        return R.success();
    }


    @Operation(summary = "修改是否显示到特护单")
    @PutMapping("/care")
    public R updateIsShowCare(@RequestBody UpdateIsShowCareVO updateIsShowCareVO) {
        int rows = bHl7YzYaoPlanService.updateIsShowCare(updateIsShowCareVO);

        if (rows > 0) {
            return R.success();
        } else {
            return R.fail("");
        }
    }


    @Operation(summary = "入量统计修改：修改实际用量 入量类型")
    @PutMapping("/ytcount")
    public R updateQtCount(@RequestBody UpdateIsShowCareVO updateIsShowCareVO) {
        int rows = bHl7YzYaoPlanService.updateYtCount(updateIsShowCareVO);
        if (rows > 0) {
            return R.success();
        } else {
            return R.fail("修改失败");
        }
    }

    @Operation(summary = "编辑的时候查询详情")
    @GetMapping("/edit/view")
    public R<BHl7YzYaoPlan> editView(String groupId, String planTime, String hospitalNumber) {
        List<BHl7YzYaoPlan> list = bHl7YzYaoPlanService.selectListByGroupIdPlanTime(groupId, planTime, hospitalNumber);
        BHl7YzYaoPlan po = new BHl7YzYaoPlan();
        if (!Collections.isEmpty(list)) {
            po = list.get(0);
            return R.success(po);
        } else {
            return R.success(po);
        }
    }

    @Operation(summary = "给药方式字典模糊查询")
    @PostMapping("/route")
    public R<List<BHl7Route>> selectRouteListByQuery(@RequestBody ListRouteQueryVO queryInVO) {
        List<BHl7Route> result = bHl7RouteService.selectRouteListByQuery(queryInVO);
        return R.success(result);
    }

//    @GetMapping("/pda/setdata")
//    public R setdata() {
//        pdaYaoJob.setData();
//        return R.success();
//    }
//
//    @Operation(summary = "批量添加pda用药记录数据")
//    @PostMapping("/pda/adddatas")
//    public R setdata(@RequestBody List<PdaInVO> pdas) {
//        pdaYaoJob.addDatas(pdas);
//        return R.success();
//    }
//
//    @Operation(summary = "查询pda用药格式的执行用药记录")
//    @GetMapping("/pda/datas")
//    public R<PageOutputDTO<PdaOutVO>> getDatas(PdaParams param) {
//        return R.success(bExeYaoEventService.selectYaoAsPdaData(param));
//    }


}


